package com.gobang.gobangboot.interceptor;

import com.gobang.gobangboot.utils.BaseContext;
import com.gobang.gobangboot.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.lang.reflect.Type;
import java.util.Map;


public class JwtTokenUserInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("jwt-token");
        System.out.println("token11:"  + token);
        try{
            Map<String, Object> cliams = JwtUtil.parseToken(token);
            Long id = ((Number) cliams.get("id")).longValue();
            BaseContext.setCurrentId(id);
            return true;
        } catch (Exception ex){
            ex.printStackTrace();
            response.setStatus(401);
            return false;
        }

    }
}
